﻿Gnc.DepartmentTreePanel = function () {
    Gnc.DepartmentTreePanel.superclass.constructor.call(this);
};
mini.extend(Gnc.DepartmentTreePanel, mini.Box, {
    //title:"组织机构",
    uiCls: "gnc-departmenttreepanel",
    treeUrl: bootPATH + 'Js/Common/Org/DepartmentTreePanelView.htm',
    storeUrl: bootPATH+GNC.Uri.GetOrganizationsTree, 
   /* storeUrl: bootPATH+GNC.Uri.GetOrganizationsTree.replace('js=1','js=2'), */
    selectedItem: null,
    tree: null,
    type:null,
    level:"root",
    isCollspanse:false,
    _create: function () {
        Gnc.DepartmentTreePanel.superclass._create.call(this);
        var html = mini.getRemoteText(this.treeUrl);
        this.setControls([html]);
        
        this.tree = mini.getbyName("department-tree",this);

        //设置click事件
        this.tree.on('nodeclick', function (obj) {
            if (this.selectedItem == obj.node) return;
            this.selectedItem = obj.node;            
            this.level=obj.node.id;
            this.fire('departmentSelected', this.selectedItem);
        }, this);
        //展开节点前发生
        this.tree.on('beforeload', function (e) {
            this.level="root";
            var params = e.params;  //参数对象
            if(!mini.isEmpty(e.node) && !mini.isEmpty(e.node.id)){
                this.level=e.node.id;
            }
            //可以传递自定义的属性
            params.node = this.level; //后台：request对象获取"myField"
        }, this);

        this.tree.on('drawcell',function(e){
            this.dataRender(e);
        },this);        
       
        //部门按钮
        var btn = mini.getbyName("btn_deptDepartment",this);
        btn.on('click',function(){
            this.loadByType("dept","department");
        },this);
        //客户按钮
        btn = mini.getbyName("btn_customerDepartment",this);
        btn.on('click',function(){
            this.loadByType("cust","customer");
        },this);
        // //外协按钮
        // btn = mini.getbyName("btn_joinedProductionsupplierDepartment",this);
        // btn.on('click',function(){
        //     this.loadByType("purchaseSuply","purchasesupplier");
        // },this);
        // //二配按钮
        // btn = mini.getbyName("btn_ComponentSubcontractorDepartment",this);
        // btn.on('click',function(){
        //     this.loadByType("ComponentSubcontractorSuply","ComponentSubcontractor");
        // },this);
        // //采购按钮
        // btn = mini.getbyName("btn_purchasesupplierDepartment",this);
        // btn.on('click',function(){
        //     this.loadByType("joinedProductionSuply","joinedProductionsupplier");
        // },this);
    },    
    //清除数据
    removeAll:function(){
        this.tree.clearData();
    },
    //按节点刷新
    reLoadNode:function(){        
        //var row = me.tree.getSelectedNode();
        this.load();
    },
    //刷新数据
    reload: function () {
        this.removeAll();
        this.tree.reload();
    },
    loadByType: function(typeId,type){
        this.typeId = typeId;
        this.type = type;
        this.level="root";
        this.load();
    },
    //加载数据
    load: function () {
        this.removeAll();
        //this.tree.setAjaxOptions({type:'GET'});
        if(!mini.isEmpty(this.type)){
            this.tree.setUrl(this.storeUrl+'&type='+this.type);
        }
    },
    dataRender:function(e){        
        var sender = e.sender;
        var node = e.node;
        //修改默认的父子节点图标
        /*if (node.leaf == true) {
            e.iconCls = "file";
        }*/
    }
    
});
mini.regClass(Gnc.DepartmentTreePanel, "Gnc.DepartmentTreePanel");    